<?php
/**
 * Created by PhpStorm.
 * User: zhangrongxiang
 * Date: 2017/3/10
 * Time: 下午5:08
 */



/**
 * 字符串截取
 * 多种编码截取
 * @param $String
 * @param $Length
 * @param bool $Append
 * @return array|string
 */
function sysSubStr($String, $Length, $Append = true)
{
    if (strlen($String) <= $Length) {
        return $String;
    } else {
        $I = 0;
        while ($I < $Length) {
            $StringTMP = substr($String, $I, 1);
            echo $StringTMP.".";
            echo ord($StringTMP).".";
            if (ord($StringTMP) >= 224) {
                $StringTMP = substr($String, $I, 3);
                $I = $I + 3;
            } elseif (ord($StringTMP) >= 192) {
                $StringTMP = substr($String, $I, 2);
                $I = $I + 2;
            } else {
                $I = $I + 1;
            }
            $StringLast[] = $StringTMP;
        }
        $StringLast = implode("", $StringLast);
        if ($Append) {
            $StringLast .= "...";
        }

        return $StringLast;
    }
}


$str = <<<ABC
<html>
    <body>
        hello world
        你好你好
    <body>
</html>
ABC;

//echo(sysSubStr($str,60,false));
echo substr($str,0,55);

echo ord("我");
echo ord("");

